#!/usr/bin/env bash
#
# Copyright 2016-2018 Axel Huebl, Rene Widera
#
# This file is part of PIConGPU.
#
# PIConGPU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PIConGPU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIConGPU.
# If not, see <http://www.gnu.org/licenses/>.
#

# Search recursive inside a folder if a shell scripts or batch system files
# contains non-ASCII characters. This causes problems with some systems.
# We can also not guarantee that everyone has UTF8 locales installed, so
# why depend on it.
#
# @result 0 if no files are found, else 1
#

ok=0

for i in $(find . \
                -not -path "./.git/*" \
                -not -path "./thirdParty/*" \
                -type f | \
           egrep "\.def$|\.h$|\.cpp$|\.cu$|\.hpp$|\.tpp$|\.kernel$|\.loader$|\
                  \.param$|\.unitless$|\.sh$|\.bash$|\.cfg$|\.tpl$|\.conf$|\
                  \.awk$|\.gnuplot$|\.cmake$|\.profile$|\.example$|\.py$")
do
  # non-ASCII test regex via jerrymouse at stackoverflow under CC-By-SA 3.0:
  #   http://stackoverflow.com/questions/3001177/how-do-i-grep-for-all-non-ascii-characters-in-unix/9395552#9395552
  result=$(grep --color='always' -P -n "[\x80-\xFF]" $i)

  if [ $? -eq 0 ]
  then
    echo "$i contains non-ASCII characters!"
    echo "$result"
    ok=1
  fi
done

exit $ok
